<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
    :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="单位名称" prop="unitName">
            <el-input v-model="form.unitName" placeholder="请输入单位名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="单位简称" prop="unitAbbr">
            <el-input v-model="form.unitAbbr" placeholder="请输入单位简称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="占地面积（万平方米）" prop="landArea">
            <el-input v-model="form.landArea" placeholder="请输入占地面积（万平方米）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="年总产值（万元）" prop="annualOutputValue">
            <el-input v-model="form.annualOutputValue" placeholder="请输入年总产值（万元）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="联系人" prop="contactPerson">
            <el-input v-model="form.contactPerson" placeholder="请输入联系人"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="form.contactPhone" placeholder="请输入联系电话"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="传真号码" prop="faxNumber">
            <el-input v-model="form.faxNumber" placeholder="请输入传真号码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="邮政编码" prop="postalCode">
            <el-input v-model="form.postalCode" placeholder="请输入邮政编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="单位网址" prop="unitWebsite">
            <el-input v-model="form.unitWebsite" placeholder="请输入单位网址"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="专职环保人员数" prop="fullTimeEnvStaff">
            <el-input v-model="form.fullTimeEnvStaff" placeholder="请输入专职环保人员数"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="年末职工总数" prop="totalEmployees">
            <el-input v-model="form.totalEmployees" placeholder="请输入年末职工总数"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="环保负责人" prop="environmentalLeader">
            <el-input v-model="form.environmentalLeader" placeholder="请输入环保负责人"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="环保负责人电话" prop="environmentalLeaderPhone">
            <el-input v-model="form.environmentalLeaderPhone" placeholder="请输入环保负责人电话"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="统一社会信用代码" prop="unifiedSocialCreditCode">
            <el-input v-model="form.unifiedSocialCreditCode" placeholder="请输入统一社会信用代码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="单位法人（法人代表）" prop="legalRepresentative">
            <el-input v-model="form.legalRepresentative" placeholder="请输入单位法人（法人代表）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="法定代表人联系电话" prop="legalRepresentativePhone">
            <el-input v-model="form.legalRepresentativePhone" placeholder="请输入法定代表人联系电话"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="工商注册地行政区" prop="businessRegistrationArea">
            <el-input v-model="form.businessRegistrationArea" placeholder="请输入工商注册地行政区"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="工商注册地址" prop="businessRegistrationAddress">
            <el-input v-model="form.businessRegistrationAddress" placeholder="请输入工商注册地址"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="所属行业" prop="industry">
            <el-input v-model="form.industry" placeholder="请输入所属行业"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="单位类型" prop="unitType">
            <el-input v-model="form.unitType" placeholder="请输入单位类型"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="注册资金（万元）" prop="registeredCapital">
            <el-input v-model="form.registeredCapital" placeholder="请输入注册资金（万元）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="产品销售额（万元）" prop="productSales">
            <el-input v-model="form.productSales" placeholder="请输入产品销售额（万元）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="总投资（万元）" prop="totalInvestment">
            <el-input v-model="form.totalInvestment" placeholder="请输入总投资（万元）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="排污许可或登记编号" prop="pollutionPermitOrRegistrationCode">
            <el-input v-model="form.pollutionPermitOrRegistrationCode" placeholder="请输入排污许可或登记编号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="涉及业务" prop="involvedBusiness">
            <el-input v-model="form.involvedBusiness" placeholder="请输入涉及业务"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="固废产废规模" prop="solidWasteProductionScale">
            <el-input v-model="form.solidWasteProductionScale" placeholder="请输入固废产废规模"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设施所在地行政区" prop="facilityLocationArea">
            <el-input v-model="form.facilityLocationArea" placeholder="请输入设施所在地行政区"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设施所在街道 (乡镇)" prop="facilityStreet">
            <el-input v-model="form.facilityStreet" placeholder="请输入设施所在街道 (乡镇)"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设施所在地生态环境局" prop="facilityEnvironmentalBureau">
            <el-input v-model="form.facilityEnvironmentalBureau" placeholder="请输入设施所在地生态环境局"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设施经度" prop="facilityLongitude">
            <el-input v-model="form.facilityLongitude" placeholder="请输入设施经度"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设施纬度" prop="facilityLatitude">
            <el-input v-model="form.facilityLatitude" placeholder="请输入设施纬度"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="设施地址" prop="facilityAddress">
            <el-input v-model="form.facilityAddress" placeholder="请输入设施地址"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="环评文件" prop="environmentalImpactAssessmentFiles">
            <el-input v-model="form.environmentalImpactAssessmentFiles" placeholder="请输入环评文件"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="环评批复" prop="environmentalImpactAssessmentApprovalFiles">
            <el-input v-model="form.environmentalImpactAssessmentApprovalFiles" placeholder="请输入环评批复"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="营业执照" prop="businessLicenseFiles">
            <el-input v-model="form.businessLicenseFiles" placeholder="请输入营业执照"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="组织机构代码证（多张图片）" prop="organizationCodeFiles">
            <el-input v-model="form.organizationCodeFiles" placeholder="请输入组织机构代码证（多张图片）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="排污许可证照（单张图片）" prop="pollutionPermitFiles">
            <el-input v-model="form.pollutionPermitFiles" placeholder="请输入排污许可证照（单张图片）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="其他证件（多张图片）" prop="otherDocumentsFiles">
            <el-input v-model="form.otherDocumentsFiles" placeholder="请输入其他证件（多张图片）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="直接负责人手机号（必填项）" prop="directResponsiblePersonPhone">
            <el-input v-model="form.directResponsiblePersonPhone" placeholder="请输入直接负责人手机号（必填项）"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="营业执照期限" prop="businessLicenseExpiry">
            <el-input v-model="form.businessLicenseExpiry" placeholder="请输入营业执照期限"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="组织机构代码证期限" prop="organizationCodeExpiry">
            <el-input v-model="form.organizationCodeExpiry" placeholder="请输入组织机构代码证期限"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="排污许可证期限" prop="pollutionPermitExpiry">
            <el-input v-model="form.pollutionPermitExpiry" placeholder="请输入排污许可证期限"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="EnterpriseInfoDialog">
// ========== 1. 导入语句 ==========
import { useDict } from '/@/hooks/dict';
import { rule } from '/@/utils/validate';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, validateExist } from '/@/api/gfadmin/enterpriseInfo';

// ========== 2. 组件定义 ==========
// 定义组件事件
const emit = defineEmits(['refresh']);

// ========== 3. 响应式数据定义 ==========
// 基础响应式变量
const dataFormRef = ref(); // 表单引用
const visible = ref(false); // 弹窗显示状态
const loading = ref(false); // 加载状态

// 表单数据对象
const form = reactive({
  id: '', // 主键
  unitName: '', // 单位名称
  unitAbbr: '', // 单位简称
  landArea: '', // 占地面积（万平方米）
  annualOutputValue: '', // 年总产值（万元）
  contactPerson: '', // 联系人
  contactPhone: '', // 联系电话
  faxNumber: '', // 传真号码
  postalCode: '', // 邮政编码
  email: '', // 邮箱
  unitWebsite: '', // 单位网址
  fullTimeEnvStaff: '', // 专职环保人员数
  totalEmployees: '', // 年末职工总数
  environmentalLeader: '', // 环保负责人
  environmentalLeaderPhone: '', // 环保负责人电话
  unifiedSocialCreditCode: '', // 统一社会信用代码
  legalRepresentative: '', // 单位法人（法人代表）
  legalRepresentativePhone: '', // 法定代表人联系电话
  businessRegistrationArea: '', // 工商注册地行政区
  businessRegistrationAddress: '', // 工商注册地址
  industry: '', // 所属行业
  unitType: '', // 单位类型
  registeredCapital: '', // 注册资金（万元）
  productSales: '', // 产品销售额（万元）
  totalInvestment: '', // 总投资（万元）
  pollutionPermitOrRegistrationCode: '', // 排污许可或登记编号
  involvedBusiness: '', // 涉及业务
  solidWasteProductionScale: '', // 固废产废规模
  facilityLocationArea: '', // 设施所在地行政区
  facilityStreet: '', // 设施所在街道 (乡镇)
  facilityEnvironmentalBureau: '', // 设施所在地生态环境局
  facilityLongitude: '', // 设施经度
  facilityLatitude: '', // 设施纬度
  facilityAddress: '', // 设施地址
  environmentalImpactAssessmentFiles: '', // 环评文件
  environmentalImpactAssessmentApprovalFiles: '', // 环评批复
  businessLicenseFiles: '', // 营业执照
  organizationCodeFiles: '', // 组织机构代码证（多张图片）
  pollutionPermitFiles: '', // 排污许可证照（单张图片）
  otherDocumentsFiles: '', // 其他证件（多张图片）
  directResponsiblePersonPhone: '', // 直接负责人手机号（必填项）
  businessLicenseExpiry: '', // 营业执照期限
  organizationCodeExpiry: '', // 组织机构代码证期限
  pollutionPermitExpiry: '', // 排污许可证期限
});

// ========== 4. 字典数据处理 ==========

// ========== 5. 表单校验规则 ==========
const dataRules = ref({
});

// ========== 6. 方法定义 ==========
// 获取详情数据
const getEnterpriseInfoData = async (id: string) => {
  try {
    loading.value = true;
    const { data } = await getObj({ id: id });
    // 直接将第一条数据赋值给表单
    Object.assign(form, data[0]);
  } catch (error) {
    useMessage().error('获取数据失败');
  } finally {
    loading.value = false;
  }
};

// 打开弹窗方法
const openDialog = (id: string) => {
  visible.value = true;
  form.id = '';

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取EnterpriseInfo信息
  if (id) {
    form.id = id;
    getEnterpriseInfoData(id);
  }
};

// 提交表单方法
const onSubmit = async () => {
  loading.value = true; // 防止重复提交
  
  // 表单校验
  const valid = await dataFormRef.value.validate().catch(() => {});
  if (!valid) {
    loading.value = false;
    return false;
  }

  try {
    // 根据是否有ID判断是新增还是修改
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh'); // 通知父组件刷新列表
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// ========== 7. 对外暴露 ==========
// 暴露方法给父组件
defineExpose({
  openDialog
});
</script> 